package com.sdu.shanyiapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.sdu.shanyiapi.common.R;
import com.sdu.shanyiapi.controller.form.LoginOrRegisterForm;
import com.sdu.shanyiapi.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lihaopeng
 * @version 1.0
 * @project shanyi-api
 * @description 用户
 * @since 2024/4/13 12:25
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/loginOrRegister")
    public R loginOrRegister(@RequestBody @Valid LoginOrRegisterForm form) {
        HashMap map = userService.loginOrRegister(form.getCode(), form.getNickName(), form.getAvatar(), form.getSex());
        String msg = MapUtil.getStr(map, "msg");
        String id = MapUtil.getStr(map, "id");
        StpUtil.login(id);
        String token = StpUtil.getTokenValue();
        return R.ok(msg).put("token", token).put("phone", MapUtil.getStr(map, "phone"));
    }

    @GetMapping("/searchUserSimpleInfoById")
    @SaCheckLogin
    public R searchUserSimpleInfoById() {
        int userId = StpUtil.getLoginIdAsInt();
        HashMap map = userService.searchUserSimpleInfoById(userId);
        return R.ok().put("result", map);
    }

}
